package com.jingding.procurement.web;


import com.jingding.procurement.ex.ServiceException;
import com.jingding.procurement.restful.ServiceCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class JsonResult {
    /**
     * 业务状态码
     */
    private Integer code;
    private String message;
    private Object data;

    public static JsonResult ok() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK.getCode();
        jsonResult.message = ServiceCode.OK.getMessage();
        return jsonResult;
    }
    public static JsonResult ok(String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK.getCode();
        jsonResult.message = message;
        return jsonResult;
    }
    public static JsonResult ok(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK.getCode();
        jsonResult.message = ServiceCode.OK.getMessage();
        jsonResult.data = data;
        return jsonResult;
    }


    public static JsonResult fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(Integer code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code;
        jsonResult.message = message;
        return jsonResult;
    }


}
